package com.module.overview.ui.schedule.provider

import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.KeyPre
import com.common.app.data.bean.overView.CardSettingModel
import com.common.app.helper.RouterHelper
import com.common.app.utls.TimeUtils
import com.common.base.app.extras.doOnIOThread
import com.common.base.app.extras.doOnUiThread
import com.common.base.app.extras.setSafeOnClickListener
import com.common.base.app.extras.setVisible
import com.common.base.app.extras.yes
import com.common.base.utils.AppUtils
import com.common.base.utils.AppUtils.getString
import com.common.base.utils.JsonUtils
import com.common.base.utils.MathUtil
import com.common.base.utils.SpUtilsManagement
import com.common.base.utils.SpanUtils
import com.common.base.utils.ViewUtils
import com.module.ble.repo.SportRecordRepo
import com.module.ble.repo.UserCalorieRepo
import com.module.ble.repo.UserStepsRelatedRecordRepo
import com.module.ble.utils.AllCardTypeConfig
import com.module.overview.R
import com.module.overview.ui.widget.AnimatedTripleRingView
import java.util.concurrent.TimeUnit

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/6/16
 *
 * 描述：活动卡片
 *
 * 修订历史：完成度100%
 *
 */
class OvMainActivityCardProvider(
    override val itemViewType: Int = AllCardTypeConfig.TYPE_CARD_ACTIVITY,
    override val layoutId: Int = R.layout.overview_main_activity_crad_provider
) : BaseItemProvider<CardSettingModel>() {

    private var tripleRingView: AnimatedTripleRingView? = null
    private var tvActivitySteps:TextView? = null
    private var tvActivityCalorie:TextView? = null
    private var tvActivityDuration:TextView? = null

    var stepsProgress = 0f
    var calorieProgress = 0f
    var exerciseDurationProgress = 0f

    init {
    }

    override fun convert(helper: BaseViewHolder, item: CardSettingModel) {

        helper.getView<View>(R.id.totalActivityCard).setSafeOnClickListener {
            RouterHelper.navigationPostcard(RouterHub.ROUTER_OVERVIEW_CARD_HISTORY_DETAILS)
                .withString(KeyBundle.BUNDLE_TITLE, item.name)
                .navigation()
        }

        helper.itemView.setVisible(item.isEnabled)
        tvActivitySteps?.let {
            SpanUtils.create()
                .addForeColorSection(
                    getString(R.string.common_rail_unit),
                    ViewUtils.getColor(R.color.c_191919)
                )
                .setAbsSize(getString(R.string.common_rail_unit), 16)
                .setStyle(getString(R.string.common_rail_unit), Typeface.BOLD)
                .addSection(getString(R.string.common_step_splice))
                .showIn(it)
        }

        tvActivityCalorie?.let {
            SpanUtils.create()
                .addForeColorSection(
                    getString(R.string.common_rail_unit),
                    ViewUtils.getColor(R.color.c_191919)
                )
                .setAbsSize(getString(R.string.common_rail_unit), 16)
                .setStyle(getString(R.string.common_rail_unit), Typeface.BOLD)
                .addSection(getString(R.string.common_calorie_splice))
                .showIn(it)
        }

        tvActivityDuration?.let {
            SpanUtils.create()
                .addForeColorSection(
                    getString(R.string.common_rail_unit),
                    ViewUtils.getColor(R.color.c_191919)
                )
                .setAbsSize(getString(R.string.common_rail_unit), 16)
                .setStyle(getString(R.string.common_rail_unit), Typeface.BOLD)
                .addSection(getString(R.string.common_hours_hint))
                .addForeColorSection(
                    getString(R.string.common_rail_unit),
                    ViewUtils.getColor(R.color.c_191919)
                )
                .setAbsSize(getString(R.string.common_rail_unit), 16)
                .setStyle(getString(R.string.common_rail_unit), Typeface.BOLD)
                .addSection(getString(R.string.common_minutes_hint)) //添加普通字符串
                .showIn(it)
        }

        getTheDayActivityData()
    }

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        tripleRingView = viewHolder.getView(R.id.tripleRingView)
        tvActivitySteps = viewHolder.getView(R.id.tvActivitySteps)
        tvActivityCalorie = viewHolder.getView(R.id.tvActivityCalorie)
        tvActivityDuration = viewHolder.getView(R.id.tvActivityDuration)
        super.onViewHolderCreated(viewHolder, viewType)
    }

    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)
    }


    /**
     * 获取当天活动信息
     */
    fun getTheDayActivityData(){
        doOnIOThread {
            UserStepsRelatedRecordRepo.instance.findByOneDay(TimeUtils.getTimeDateLong())?.let { originalListIt->

                var totalStepsNum = 0L

                originalListIt.forEach { beanIt->
                    totalStepsNum += beanIt.step.toInt()
                }

                doOnUiThread {

                    tvActivitySteps?.let {
                        SpanUtils.create()
                            .addForeColorSection(
                                totalStepsNum.toString(),
                                ViewUtils.getColor(R.color.c_191919)
                            )
                            .setAbsSize(totalStepsNum.toString(), 16)
                            .setStyle(totalStepsNum.toString(), Typeface.BOLD)
                            .addSection(getString(R.string.common_step_splice))
                            .showIn(it)
                    }

                    stepsProgress = (MathUtil.div9(totalStepsNum.toString(),SpUtilsManagement.getInt(KeyPre.KEY_STEP_TARGET_VALUE, 6000).toString()) * 100).toFloat()

                    tripleRingView?.setProgressWithAnimation(listOf(stepsProgress, calorieProgress, exerciseDurationProgress))
                }
            }
        }

        getTheDayCalorieData()
    }

    /**
     * 获取当天卡路里信息
     */
    fun getTheDayCalorieData(){
        doOnIOThread {
            UserCalorieRepo.instance.findByOneDay(TimeUtils.getTimeDateLong())?.let { originalListIt->

                var totalCalorie = 0L

                originalListIt.forEach { beanIt->
                    totalCalorie += beanIt.calorie.toLong()
                }

                doOnUiThread {

                    tvActivityCalorie?.let {
                        SpanUtils.create()
                            .addForeColorSection(
                                totalCalorie.toString(),
                                ViewUtils.getColor(R.color.c_191919)
                            )
                            .setAbsSize(totalCalorie.toString(), 16)
                            .setStyle(totalCalorie.toString(), Typeface.BOLD)
                            .addSection(getString(R.string.common_calorie_splice))
                            .showIn(it)
                    }

                    calorieProgress = (MathUtil.div9(totalCalorie.toString(),SpUtilsManagement.getInt(KeyPre.KEY_CALORIE_TARGET_VALUE, 300).toString()) * 100).toFloat()

                    tripleRingView?.setProgressWithAnimation(listOf(stepsProgress, calorieProgress, exerciseDurationProgress))
                }
            }
        }

        getTheDaySportsData()
    }

    /**
     * 获取当天运动信息
     */
    fun getTheDaySportsData(){
        doOnIOThread {
            SportRecordRepo.instance.findAllByDay(TimeUtils.getTimeDateLong())?.let { originalListIt->
                if (originalListIt.isNullOrEmpty()) return@let
//                val listItStr = JsonUtils.toJson(originalListIt)
                var totalExerciseDuration = 0L
                originalListIt.forEach { beanIt->
                    (beanIt.startTs>0 && beanIt.endTs>0).yes {
                        totalExerciseDuration+= beanIt.endTs-beanIt.startTs
                    }
                }
                exerciseDurationProgress = (MathUtil.div9(TimeUnit.MILLISECONDS.toMinutes(totalExerciseDuration).toString(),SpUtilsManagement.getInt(KeyPre.KEY_AC_DURATION_TARGET_VALUE, 45).toString()) * 100).toFloat()

                val tvLastSList = TimeUtils.formatTimeToList(totalExerciseDuration)

                doOnUiThread {
                    tvActivityDuration?.let { viewIt ->
                        val spanBuilderTemp = SpanUtils.create()
                        (tvLastSList[1]>0).yes {
                            spanBuilderTemp
                                .addForeColorSection(
                                    tvLastSList[1].toString(),
                                    ViewUtils.getColor(R.color.c_191919)
                                )
                                .setAbsSize(tvLastSList[1].toString(), 16)
                                .setStyle(tvLastSList[1].toString(), Typeface.BOLD)
                                .addSection(getString(R.string.common_hours_hint))
                        }
                        spanBuilderTemp
                            .addForeColorSection(
                                tvLastSList[2].toString(),
                                ViewUtils.getColor(R.color.c_191919)
                            )
                            .setAbsSize(tvLastSList[2].toString(), 16)
                            .setStyle(tvLastSList[1].toString(), Typeface.BOLD)
                            .addSection(getString(R.string.common_minutes_hint))
                            .showIn(viewIt)
                    }

                    tripleRingView?.setProgressWithAnimation(listOf(stepsProgress, calorieProgress, exerciseDurationProgress))
                }
            }
        }
    }
}